
package rentalgui;

import database.LeaseDao;
import database.PaymentDao;
import database.TenantDao;
import javax.swing.JOptionPane;
import rentaldb.Tenant;

public class TenantUpdateDeleteWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseUpdateDeleteWindow
     */
    public TenantUpdateDeleteWindow(LeaseDao ldao, PaymentDao paydao, TenantDao tdao) {
        initComponents();
        updateMode = false;
        this.ldao = ldao;
        this.paydao = paydao;
        this.tdao = tdao;      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        inputBox = new javax.swing.JTextField();
        clearButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        inputWorkPhone = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        inputCity = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        inputState = new javax.swing.JTextField();
        inputMobilePhone = new javax.swing.JTextField();
        inputZip = new javax.swing.JTextField();
        inputAddress = new javax.swing.JTextField();
        inputLastName = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        inputEmail = new javax.swing.JTextField();
        inputHomePhone = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        inputTenantId = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        inputFirstName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tenant Update/Delete");

        jLabel7.setText("Tenant ID");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        updateButton.setText("Update");
        updateButton.setEnabled(false);
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        jLabel8.setText("Work Phone");

        inputWorkPhone.setEditable(false);

        jLabel9.setText("Zip");

        jLabel6.setText("State");

        inputCity.setEditable(false);

        jLabel1.setText("Tenant ID");

        jLabel12.setText("Address");

        jLabel2.setText("First Name");

        inputState.setEditable(false);

        inputMobilePhone.setEditable(false);

        inputZip.setEditable(false);

        inputAddress.setEditable(false);

        inputLastName.setEditable(false);

        jLabel13.setText("City");

        inputEmail.setEditable(false);

        inputHomePhone.setEditable(false);

        jLabel14.setText("Mobile Phone");

        inputTenantId.setEditable(false);

        jLabel5.setText("Home Phone");

        jLabel3.setText("Last Name");

        inputFirstName.setEditable(false);

        jLabel4.setText("Email");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputBox)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputZip, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputState, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputCity, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputAddress, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputWorkPhone, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputMobilePhone, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputHomePhone, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputEmail, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputLastName, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputFirstName, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputTenantId))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(inputBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(clearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(inputTenantId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(inputHomePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputMobilePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputWorkPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(inputCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(inputState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(inputZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exitButton)
                    .addComponent(updateButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        clearTextBoxes();
        setEditableStatus(false);
        updateMode = false;
        updateButton.setEnabled(false);
        deleteButton.setEnabled(false);
    }//GEN-LAST:event_clearButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try{
            Integer input = Integer.parseInt(inputBox.getText().trim());
            curTenant = tdao.getTenantById(input);
            putCurrentInTextBoxes();
            setEditableStatus(true);
            updateMode = false;
            updateButton.setEnabled(true);
            deleteButton.setEnabled(true);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Invalid ID.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_searchButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this entry?", "Delete?", JOptionPane.YES_NO_OPTION);
        if(input == JOptionPane.YES_OPTION){
            try{
                tdao.delete(curTenant);
                clearButtonActionPerformed(evt);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, "Error during delete.", "Delete Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        if(!updateMode){
            updateMode = true;
            JOptionPane.showMessageDialog(null, "Make the desired changes to the fields, then click update again.", "Update Mode", JOptionPane.INFORMATION_MESSAGE);
            deleteButton.setEnabled(false);
            setEditableStatus(true);
        }else{
            updateMode = false;
            deleteButton.setEnabled(true);
            int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this entry?", "Update?", JOptionPane.YES_NO_OPTION);
            if(input == JOptionPane.YES_OPTION){
                try{
                    Integer tenantId = Integer.parseInt(inputTenantId.getText().trim());
                    String fname = inputFirstName.getText().trim();
                    String lname = inputLastName.getText().trim();
                    String email = inputEmail.getText().trim();
                    String hphone = inputHomePhone.getText().trim();
                    String mphone = inputMobilePhone.getText().trim();
                    String wphone = inputWorkPhone.getText().trim();
                    String address = inputAddress.getText().trim();
                    String city = inputCity.getText().trim();
                    String state = inputState.getText().trim();
                    Integer zip = Integer.parseInt(inputZip.getText().trim());

                    Tenant tenant = new Tenant();
                    tenant.setTenantId(tenantId);
                    tenant.setFname(fname);
                    tenant.setLname(lname);
                    tenant.setEmail(email);
                    tenant.setHomePhone(hphone);
                    tenant.setMobilePhone(mphone);
                    tenant.setWorkPhone(wphone);
                    tenant.setAddress(address);
                    tenant.setCity(city);
                    tenant.setState(state);
                    tenant.setZip(zip);
                    tdao.update(tenant);
                }
                catch(Exception e){
                    JOptionPane.showMessageDialog(null, "Error during update.", "Update Error", JOptionPane.ERROR_MESSAGE);
                }
            } else{
                JOptionPane.showMessageDialog(this, "Changes Reset", "Update Cancelled", JOptionPane.INFORMATION_MESSAGE);
                putCurrentInTextBoxes();
            }
            deleteButton.setEnabled(false);
            setEditableStatus(false);
        }
    }//GEN-LAST:event_updateButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void setEditableStatus(boolean setting){
        inputBox.setEditable(setting);
        inputFirstName.setEditable(setting);
        inputLastName.setEditable(setting);
        inputEmail.setEditable(setting);
        inputHomePhone.setEditable(setting);
        inputMobilePhone.setEditable(setting);
        inputWorkPhone.setEditable(setting);
        inputAddress.setEditable(setting);
        inputCity.setEditable(setting);
        inputState.setEditable(setting);
        inputZip.setEditable(setting);
    }
    
    private void clearTextBoxes(){
        inputBox.setText("");
        inputTenantId.setText("");
        inputFirstName.setText("");
        inputLastName.setText("");
        inputEmail.setText("");
        inputHomePhone.setText("");
        inputMobilePhone.setText("");
        inputWorkPhone.setText("");
        inputAddress.setText("");
        inputCity.setText("");
        inputState.setText("");
        inputZip.setText("");
    }
    
    private void putCurrentInTextBoxes(){
        inputTenantId.setText(curTenant.getTenantId().toString().trim());
        inputFirstName.setText(curTenant.getFname().toString().trim());
        inputLastName.setText(curTenant.getLname().toString().trim());
        inputEmail.setText(curTenant.getEmail().toString().trim());
        inputHomePhone.setText(curTenant.getHomePhone().toString().trim());
        inputMobilePhone.setText(curTenant.getMobilePhone().toString().trim());
        inputWorkPhone.setText(curTenant.getWorkPhone().toString().trim());
        inputAddress.setText(curTenant.getAddress().toString().trim());
        inputCity.setText(curTenant.getCity().toString().trim());
        inputState.setText(curTenant.getState().toString().trim());
        inputZip.setText(curTenant.getZip().toString().trim());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JTextField inputAddress;
    private javax.swing.JTextField inputBox;
    private javax.swing.JTextField inputCity;
    private javax.swing.JTextField inputEmail;
    private javax.swing.JTextField inputFirstName;
    private javax.swing.JTextField inputHomePhone;
    private javax.swing.JTextField inputLastName;
    private javax.swing.JTextField inputMobilePhone;
    private javax.swing.JTextField inputState;
    private javax.swing.JTextField inputTenantId;
    private javax.swing.JTextField inputWorkPhone;
    private javax.swing.JTextField inputZip;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
    private Tenant curTenant;
    private LeaseDao ldao;
    private PaymentDao paydao;
    private TenantDao tdao;
    private boolean updateMode;
}
